<?php
	$validation_rules = array(
		"onair_episode_no" => array(
			"required" => true,
            'positiveInt' => true,
            'epsExist' => true,
            'maxlength' => 9
		),
        'programme_title_cn' => array(
            'maxlength' => 255
        ),
        'programme_title_en' => array(
            'maxlength' => 255
        ),
        'programme_path' => array(
            'required' => true,
            #'path' => true,
            'maxlength' => 255
        ),
        "name_chinese" => array(
			'maxlength' => '255'
		),
		"name_english" => array(
			'maxlength' => '255'
		),
        'country_autocomplete' => array(
            'inAcList' => true
        ),
        'province_state_autocomplete' => array(
            'inAcList' => true
        ),
		'pageant_no'	=> array(
			'maxlength'	=> 9,
			'number'	=> TRUE,
		),
		"date_of_birth_year" => array(
			'maxlength' => '9',
			'number' => true,
			'positiveInt' => true
		),
		"date_of_birth_month" => array(
			'maxlength' => '9',
			'number' => true,
		),
		"date_of_birth_day" => array(
			'maxlength' => '9',
			'number' => true,
		),
		"origin_city" => array(
			'maxlength' => '255'
		),
		"occupation" => array(
			'maxlength' => '255'
        ),
        'publish_datetime[]' => array(
            'required' => TRUE
        ),
        'expire_datetime[]' => array(
            'required' => TRUE
        ),
	);
?>

<?php
    if ($act==='view') {
        FormHelper::setReadonly(true);
        $validation_rules = array();
		PDES_GD::$astrology[0] = '';
		PDES_GD::$zodiac[0] = '';
    }
	
	$astrology_arr = PDES_GD::$astrology;
	$zodiac_arr = PDES_GD::$zodiac;

	$pageant = isset($data['pageant']) ? $data['pageant'] : array();
	$pageant->date_of_birth_year = ($pageant->date_of_birth_year == 0) ? '' : $pageant->date_of_birth_year;
	$pageant->date_of_birth_month = ($pageant->date_of_birth_month == 0) ? '' 
									: str_pad($pageant->date_of_birth_month,2,'0',STR_PAD_LEFT);
	$pageant->date_of_birth_day = ($pageant->date_of_birth_day == 0) ? '' 
								  : str_pad($pageant->date_of_birth_day,2,'0',STR_PAD_LEFT);
	$episode = isset($data['episode']) ? $data['episode'] : "";
	$programme = isset($data['programme']) ? $data['programme'] : "";
	$form_action = ($act === 'view') ? "info_switch.html?type=pageant" : 
				   ( ($act === 'add') ? "add_pageant.html?type=pageant&act=add" : "update_pageant.html?type=pageant&act=update");
	$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;
	$from    = isset($data['from']) ? $data['from'] : '';
?>

<div id="pageant" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">

    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo $act_text; ?> Pageant</h2></div>

    <div class="bd">
    <?php FormHelper::StartForm('ane', AC::PDES_BASE_URL.$form_action) ?>
		    <?php FormHelper::ErrorMessages($errors) ?>
			<?php if($act != 'add') FormHelper::HiddenField('id', $pageant->id) ?>
    		<?php if($act != 'add') FormHelper::HiddenField('episode_id', $pageant->episode_id) ?>
			<?php FormHelper::HiddenField('check_used', $data['check_used']) ?>
    		<?php FormHelper::HiddenField('status', $pageant->status) ?>
    		<?php FormHelper::HiddenField('created_at', $pageant->created_at) ?>
    		<?php FormHelper::HiddenField('created_by', $pageant->created_by) ?>
			<?php FormHelper::HiddenField('modified_at', $pageant->modified_at) ?>
			<?php FormHelper::HiddenField('modified_by', $pageant->modified_by) ?>
		    <?php FormHelper::HiddenField('from', $from) ?>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Find Programme Record</legend>
<?php
	if ($act != 'add')
	{
?>
			<?php FormHelper::ProtectedField($programme->id, 'Programme ID');?>
            <?php FormHelper::ProtectedField($pageant->id, 'Pageant ID');?>
            <?php 
				FormHelper::ProtectedField($programme->zh_programme_name, 'Programme Title (Chinese)');
				FormHelper::ProtectedField($programme->en_programme_name, 'Programme Title (English)');
				FormHelper::ProtectedField($programme->path, 'Programme Path') ?>
			<?php FormHelper::ProtectedField($episode->onair_episode_no, 'On Air Episode No.') ?>
			<?php FormHelper::HiddenField('programme_id', $programme->id) ?>
			<?php FormHelper::HiddenField('onair_episode_no', $episode->onair_episode_no) ?>
<?php
	}
	else
	{
?>
            <input id="programme_id" name="programme_id" type="hidden" value="<?php echo $pageant->programme_id;?>" />
			<input id="programme_eps" type="hidden" value="<?php echo $pageant->onair_episode_no;?>" />

            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (Chinese)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_cn" name="programme_title_cn" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($pageant->programme_title_cn, ENT_QUOTES, 'UTF-8');?>" />
                </span>
            </div>
            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (English)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_en" name="programme_title_en" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($pageant->programme_title_en, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <div class="form-item">
                <span class="grid-l"><label><ins>*</ins>Programme Path</label> :</span>
                <span class="grid-r">
                <input id="programme_path" name="programme_path" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($pageant->programme_path, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <?php FormHelper::TextField('onair_episode_no', $pageant->onair_episode_no, "On Air Episode No.", '', 'number', 1) ?>
<?php
	}
?>
	</fieldset>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Pageant Information</legend>
			<?php 
				FormHelper::TextField('name_chinese', $pageant->name_chinese, 'Name (Chinese)', false, 'ruler input_status', 2);
				FormHelper::TextField('name_english', $pageant->name_english, 'Name (English)', false, 'ruler input_status', 2);					  
				FormHelper::TextField('nickname', $pageant->nickname, 'Nickname');
				FormHelper::RadioButton('gender', array('0'=>'Male','1'=>'Female'), $pageant->gender, 'Gender');
				FormHelper::TextField('pageant_no', $pageant->pageant_no, 'Pageant No.');
			 ?>
			<div class="form-item">
                <span class="grid-l"><label>Date of Birth</label> :</span>
                <span class="grid-r">
				<input type="text" maxlength="4" id="date_of_birth_year" name="date_of_birth_year" value="<?php echo $pageant->date_of_birth_year;?>" style="width:50px;"/>YYYY
				<input type="text" maxlength="2" id="date_of_birth_month" name="date_of_birth_month" value="<?php echo $pageant->date_of_birth_month;?>" style="width:25px;"/>MM
				<input type="text" maxlength="2" id="date_of_birth_day" name="date_of_birth_day" value="<?php echo $pageant->date_of_birth_day;?>" style="width:25px;"/>DD
				</span>
            </div>
			<div class="form-item">
                <span class="grid-l"><label>Origin Country</label> :</span>
                <span class="grid-r">
				<input type="hidden" id="country" name="country" value="<?php echo $pageant->origin_country;?>" />
				<input id="country_autocomplete" name="country_autocomplete" type="text" class="text" autocomplete="off" value="<?php echo $pageant->country_name;?>" />
                </span>
            </div>

            <div class="form-item">
                <span class="grid-l"><label>Origin Province/State/Prefecture</label>:</span>
                <span class="grid-r">
				<input type="hidden" id="province_state" name="province_state" value="<?php echo $pageant->origin_province_state_prefecture;?>" />
				<input id="province_state_autocomplete" name="province_state_autocomplete" type="text" class="text" autocomplete="off" value="<?php echo $pageant->province_name;?>" /> 
                </span>
            </div>
			<?php 
				FormHelper::TextField('origin_city', $pageant->origin_city, 'Origin City');
				FormHelper::DropDownList('astrology', $astrology_arr, $pageant->astrology, 'Astrology 生肖');
				FormHelper::DropDownList('zodiac', $zodiac_arr, $pageant->zodiac, 'Zodiac 星座');				
				FormHelper::TextField('occupation',  $pageant->occupation, 'Occupation');
				FormHelper::TextField('education',  $pageant->education, 'Education');
				FormHelper::TextField('height',  $pageant->height, 'Height');
                FormHelper::TextField('weight',  $pageant->weight, 'Weight');	
                FormHelper::TextField('measurements',  $pageant->measurements, 'Measurements');
                FormHelper::TextField('aspiration',  $pageant->aspiration, 'Aspiration');
                FormHelper::TextField('characters',  $pageant->characters, 'Characters');
                FormHelper::TextField('language_spoken',  $pageant->language_spoken, 'Language Spoke');
                FormHelper::TextField('hobbies',  $pageant->hobbies, 'Hobbies');
                FormHelper::TextField('talent',  $pageant->talent, 'Talent');	
				FormHelper::TextArea('description', $pageant->description, 'Description');
				FormHelper::TextArea('remark', $pageant->remark, 'Remark');
				
                if(in_array($data['_gn'],array('admin','proofreader','editor'))){
                    if (!is_array($pageant->publish_datetime)) {
                        $pageant->publish_datetime = CommonUtils::formatDate($pageant->publish_datetime);
                    }
					if (!is_array($pageant->expire_datetime)) {
                        $pageant->expire_datetime = CommonUtils::formatDate($pageant->expire_datetime);
					}
			        FormHelper::DateTimePicker('publish_datetime', 
											   $pageant->publish_datetime, 
											   'Publish Date/Time (YYYY-MM-DD / HH:MM)', NULL, 1);
			        FormHelper::DateTimePicker('expire_datetime', 
											   $pageant->expire_datetime, 
											   'Expiry Date/Time (YYYY-MM-DD / HH:MM)', NULL, 1);
                }
            ?>
		</fieldset>
<?php
	if ($act != "add")
	{
?>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Record Status and Access Log</legend>
        	    <?php FormHelper::ProtectedField(AC::$op_status[$pageant->status], 'Record Status', true) ?>
			    <?php FormHelper::ProtectedField($pageant->created_by, 'Created By') ?>
        	    <?php FormHelper::ProtectedField($pageant->created_at, 'Created At') ?>
         	    <?php FormHelper::ProtectedField($pageant->modified_by, 'Modified By') ?>       	
        	    <?php FormHelper::ProtectedField($pageant->modified_at, 'Modified At') ?>

		</fieldset>
<?php
	}
?>
        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
        <span class="buttons">
            <?php
                switch ($act) {
                    case 'add':
                        FormHelper::SubmitButton('create', 'Create'); 
                        FormHelper::ResetButton('reset', 'Reset');
                    break;
                    case 'edit':
                        FormHelper::SubmitButton('update', 'Update');
                        FormHelper::ResetButton('reset', 'Reset');
                    	FormHelper::Button('back', 'Back to list');
                    break;
                    case 'view':
						/** modified by vanni for bug no:260 **/
						if($publish ==1){
                            if (isset($data['publishLog']->status)  &&  AC::STATUS_PUBLISH  ==  $data['publishLog']->status)
                            {
                                if(empty($programme->premium_flag))
                                {
                                    FormHelper::SubmitButton('unpublish', 'Unpublish');
                                }
                                else
                                {
                                    FormHelper::SubmitButton('unpublish_premium', 'Unpublish(Premium)');
                                }
                            }
                        }else{
							if($pageant->status == AC::STATUS_DELETED){
								FormHelper::SubmitButton('recover', 'Recover');
							}else{
								FormHelper::SubmitButton('edit', 'Edit');
                                if (empty($data['publishLog']->status)  ||  AC::STATUS_PUBLISH  !=  $data['publishLog']->status)
                                    FormHelper::SubmitButton('delete', 'Delete');
							}
						}
                    	FormHelper::Button('back', 'Back to list');
                    break;
                }
            ?>
        </span>
    <?php FormHelper::EndForm() ?>

    </div>

</div>

<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
</script>
